//package com.wjk.kylin.mall.gateway.exception;
//
//import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
//import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
//import com.alibaba.csp.sentinel.slots.block.BlockException;
//import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
//import com.alibaba.fastjson.JSON;
//import com.wjk.kylin.mall.common.dto.BaseOutDTO;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.codec.ServerCodecConfigurer;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.reactive.function.server.ServerResponse;
//import org.springframework.web.reactive.result.view.ViewResolver;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.List;
//
//@Slf4j
//@Component
//public class JsonSentinelGatewayBlockExceptionHandler extends SentinelGatewayBlockExceptionHandler {
//
//    public JsonSentinelGatewayBlockExceptionHandler(List<ViewResolver> viewResolvers, ServerCodecConfigurer serverCodecConfigurer) {
//        super(viewResolvers, serverCodecConfigurer);
//    }
//
//    @Override
//    public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
//        if (exchange.getResponse().isCommitted()) {
//            return Mono.error(ex);
//        } else {
//            return !BlockException.isBlockException(ex) ? Mono.error(ex) : this.handleBlockedRequest(exchange, ex).flatMap((response) -> {
//                return this.writeResponse(exchange,ex);
//            });
//        }
//    }
//
//    private Mono<ServerResponse> handleBlockedRequest(ServerWebExchange exchange, Throwable throwable) {
//        return GatewayCallbackManager.getBlockHandler().handleRequest(exchange, throwable);
//    }
//
//    /** 只需要修改此方法 */
//    private Mono<Void> writeResponse(ServerWebExchange exchange, Throwable ex) {
//        ServerHttpResponse serverHttpResponse = exchange.getResponse();
//        serverHttpResponse.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
//        serverHttpResponse.setStatusCode( HttpStatus.TOO_MANY_REQUESTS);
//
//        BaseOutDTO dto = BaseOutDTO.error(10001, "API interface limited flow by gateway.");
//        if(ex instanceof DegradeException){
//            serverHttpResponse.setStatusCode( HttpStatus.SERVICE_UNAVAILABLE);
//            dto = BaseOutDTO.error(10002, "API interface degraded by gateway.");
//        }
//        String result = JSON.toJSONString(dto);

//        log.error("API gateway error return json :{}",result);

//        DataBuffer buffer = serverHttpResponse.bufferFactory().wrap(result.getBytes(StandardCharsets.UTF_8));
//        return serverHttpResponse.writeWith(Flux.just(buffer));
//    }
//
//}
